<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>客户管理</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">
<div class="layui-body" style="padding:15px;">

    <!-- start sp-wrapper -->
    <link rel="stylesheet" href="/sb/system.css"/>
    <div class="sp-wrapper">
        <h2 class="sp-title"><span>客户管理</span></h2>
        <form method="get" action="/contact/client_list">
            <div class="sp-tool">
                <span><input type="text" placeholder="客户" name="name"><input type="text" placeholder="手机号码"
                                                                             name="mobile"><input type="submit"
                                                                                                  value="搜索"><i
                        class="add" id="add" th:attr="d-userid=0">新增客户</i></span>
            </div>
        </form>
        <div class="sp-table">
            <table>
                <thead>
                <tr>
                    <th>序号</th>
                    <th>ID</th>
                    <th>客户名称</th>
                    <th>客户编码</th>
                    <th>分类</th>
                    <th>平台ID</th>
                    <th>联系方式</th>
                    <th>地址</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="item:${lists}">
                    <td th:text="${itemStat.index+1}">1</td>
                    <td th:text="${item.id}">张三</td>
                    <td th:text="${item.name}">张三</td>
                    <td th:text="${item.number}">张三</td>
                    <td >
                        <b th:if="${item.cCategory == 99 }">直播间</b>
                        <b th:if="${item.cCategory == 0 }">其他</b>
                        <b th:if="${item.cCategory == 1 }">消费者</b>
                    </td>

                    <td th:text="${item.userId}">

                    </td>
                    <td th:text="${item.contact}">18612348888</td>
                    <td th:text="${item.address}">18612348888</td>
                    <td th:text="${item.disable==0?'正常':'禁用'}">2018-10-20 16:43:20</td>
                    <td>
                        <i th:attr="d-name=${item.name},d-contact=${item.contact},d-cLevelName=${item.cLevelName},d-status=${item.disable},d-id=${item.getId()},d-userid=${item.getUserId()}
                        ,d-number=${item.number},d-cCategory=${item.cCategory},d-province=${item.province},d-city=${item.city},d-county=${item.county},d-address=${item.address}" class="edit">修改</i>
                        <i th:attr="data-id=${item.getId()}" class="delete">删除</i>
                        <a th:href="${'/contact/adress_list?id='+item.id}">地址管理</a>
                    </td>
                </tr>
                </tbody>
            </table>
            <div class="pages">
                <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
            </div>
        </div>
    </div>

    <!-- 模板 新增/修改 -->
    <script type="text/template" id="tpl-group">
        <div class="T-box">
            <form id="sp-form" method="post">
                <input type="hidden" id="id" name="id" value="0" />
                <p><span>客户名称</span><input id="name" name="name" type="text"/></p>
                <p><span>客户编码</span><input id="number" name="number" type="text"></p>
                <p><span>分类</span>
                    <select id="cCategory" name="cCategory">
                        <option selected disabled>请选择</option>
                        <option value="1">消费者</option>
                        <option value="99">直播间</option>
                    </select>
                </p>
                <p><span>平台ID</span><input id="userId" name="userId" type="text"></p>
                <p><span>联系方式</span><input id="contact" name="contact" type="text"/></p>
                <p><span>省市区</span>
                    <input id="province" name="province" placeholder="省份" type="text" style="width: 60px"/>
                    <input id="city" name="city" placeholder="城市" type="text" style="width: 60px" />
                    <input id="county" name="county" placeholder="区县" type="text" style="width: 60px" />
                </p>
                <p><span>详细地址</span><input id="address" name="address" type="text"/></p>
                <p><span>状态</span><select id="status" name="status">
                    <option selected disabled>请选择</option>
                    <option value="0">正常</option>
                    <option value="1">禁用</option>
                </select></p>
                <p class="text" id="sb-text"></p>
            </form>
        </div>
    </script>

    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // 新增
            $('#add').click(function () {
                Tips.alert({
                    title: '新增客户',
                    content: $('#tpl-group').html(),
                    before: function () {
                        if ($('#name').val() == '') {
                            $('#sb-text').text('姓名不能为空');
                            return false;
                        }
                        if ($('#contact').val() == '') {
                            $('#sb-text').text('手机号不能为空');
                            return false;
                        }
                        if ($('#cCategory').val() == null) {
                            $('#sb-text').text('请选择分类');
                            return false;
                        }
                        if ($('#province').val() == null) {
                            $('#sb-text').text('请填写省份');
                            return false;
                        }
                        if ($('#city').val() == null) {
                            $('#sb-text').text('请填写城市');
                            return false;
                        }
                        if ($('#county').val() == null) {
                            $('#sb-text').text('请填写区县');
                            return false;
                        }
                        if ($('#address').val() == null) {
                            $('#sb-text').text('请填写详细地址');
                            return false;
                        }
                        if ($('#status').val() == null) {
                            $('#sb-text').text('请选择状态');
                            return false;
                        }
                        // if ($('#number').val() == '') {
                        //     $('#sb-text').text('客户编码不能为空');
                        //     return false;
                        // }
                        if ($('#cCategoryName').val() == '') {
                            $('#sb-text').text('分类不能为空，分类有：直播间、其他');
                            return false;
                        }

                        var data = {
                            cCategory: $('#cCategory').val(),
                            contact: $('#contact').val(),
                            name: $('#name').val(),
                            number: $('#number').val(),
                            province: $('#province').val(),
                            city: $('#city').val(),
                            address: $('#address').val(),
                            county: $('#county').val(),
                            disable: $('#status').val(),
                            type:-10,
                            userId: $('#userId').val()
                        };
                        $.ajax({
                            url: "/ajax_manage/add_client",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.alert('新增成功', function () {
                                        location.reload(true);
                                    });
                                } else {
                                    $('#sb-text').text(res.msg);
                                }
                            }
                        });
                        return false;
                    }
                });
            });
            // 修改
            $('.edit').click(function () {
                var id = $(this).attr('d-id');
                var name = $(this).attr('d-name');
                var number = $(this).attr('d-number');
                var cCategory = $(this).attr('d-cCategory');
                var userId = $(this).attr('d-userid');
                var contact = $(this).attr('d-contact');
                var status = $(this).attr('d-status');
                var province = $(this).attr('d-province');
                var city = $(this).attr('d-city');
                var county = $(this).attr('d-county');
                var address = $(this).attr('d-address');
                Tips.alert({
                    title: '修改员工',
                    content: $('#tpl-group').html(),
                    render: function () {
                        $('#name').val(name);
                        $('#number').val(number);
                        $('#cCategory').val(cCategory);
                        $('#contact').val(contact);
                        $('#status').val(status);
                        $('#userId').val(userId);
                        $('#id').val(id);
                        $('#province').val(province);
                        $('#city').val(city);
                        $('#county').val(county);
                        $('#address').val(address);

                    },
                    before: function () {
                        var data = {
                            id: id,
                            name: $('#name').val(),
                            number: $('#number').val(),
                            cCategory: $('#cCategory').val(),
                            userId: $('#userId').val(),
                            contact: $('#contact').val(),
                            disable: $('#status').val(),
                            province: $('#province').val(),
                            city: $('#city').val(),
                            county: $('#county').val(),
                            address: $('#address').val(),
                        };
                        $.ajax({
                            url: "/ajax_manage/upd_client",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.alert('修改成功', function () {
                                        location.reload(true);
                                    });
                                } else {
                                    location.reload(true);
                                }
                            }
                        });
                        return false;
                    }
                });
            });
            // 删除
            $('.delete').click(function () {
                var data = {
                    userid: $(this).attr('data-id')
                };
                Tips.confirm({
                    title: '删除员工',
                    content: '确认要删除吗？',
                    define: function () {
                        $.ajax({
                            url: "/ajax_manage/del_client",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.alert('删除成功', function () {
                                        location.reload(true);
                                    });
                                }
                            }
                        });
                    }
                });
            });
        });
    </script>
    <!-- end sp-wrapper -->

</div>
</body>
</html>
